﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SisVenda.Business;
using SisVenda.Data;
using System.Data;
using System.Collections;
using System.Reflection;
using Froggy.Data;
using SisVenda.Web.messageBox;
using SisVenda.Web.Code;

namespace SisVenda.Web.Administrador.Fn
{
    [System.ComponentModel.DataObject(true)]
    public class CadastroCargoFn
    {

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable ObterCargosID(int idCargo)
        {
            if (idCargo == 0)
                idCargo = 1;
            #region SQL
            string sql = @"SELECT *
	                            FROM Cargo
	                            WHERE IdCargo = @idCargo
	                            ORDER BY Descricao";
            #endregion
            DbCommandUtil db = new DbCommandUtil(sql);
            db.AddParameter("@idCargo", DbType.Int32, idCargo);
            return db.GetDataTable();
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable ObterCargos(string Descricao)
        {
            string desc = Descricao;
            Descricao = "%" + desc + "%";
            #region SQL
            string sql = @"SELECT *
	                            FROM Cargo
	                            WHERE Cargo.Descricao LIKE @Descricao
	                            ORDER BY Cargo.Descricao";
            #endregion
            DbCommandUtil db = new DbCommandUtil(sql);
            db.AddParameter("@Descricao", DbType.String, Descricao);
            return db.GetDataTable();
        }

        public DataTable RetornarDataTable(IQueryable pObjQuery)
        {
            DataTable _dt = new DataTable();

            //Cria o DataTable
            foreach (var item in pObjQuery)
            {
                Type t = item.GetType();
                PropertyInfo[] properties = t.GetProperties();
                for (int i = 0; i < properties.Length; i++)
                {
                    _dt.Columns.Add(properties[i].Name);
                }
                break;
            }

            IEnumerator en = (IEnumerator)pObjQuery.GetEnumerator();

            //Popula o DataTable
            while (en.MoveNext())
            {
                Type ten = en.Current.GetType();
                PropertyInfo[] propertiesen = ten.GetProperties();
                DataRow _dtrow = _dt.NewRow();
                for (int i = 0; i < propertiesen.Length; i++)
                {
                    object obj = propertiesen[i].GetValue(en.Current, new object[] { });
                    _dtrow[i] = obj;
                }
                _dt.Rows.Add(_dtrow);
            }

            return _dt;
        }


        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert)]
        public Boolean InserirCargo(String descricao, int cargahoraria, Decimal salario)
        {
            CargoBo cargos = new CargoBo();
            Cargo cargo = new Cargo();

            cargo.Descricao = descricao;
            cargo.CargaHoraria = cargahoraria;
            cargo.SalarioBase = salario;

            cargos.Add(cargo);
            try
            {
                cargos.SaveChanges();
                MessageBox.Show("Cargo incluido com sucesso!", MessageBox.MessageType.Success);
                return true;
            }
            catch
            {
                MessageBox.Show("Erro ao realizar operação!", MessageBox.MessageType.Error);
                return false;
            }
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)]
        public Boolean UpdateCargo(int idCargo, String descricao, int cargahoraria, Decimal salario)
        {

            CargoBo cargos = new CargoBo();
            Cargo cargo = new Cargo();

            cargo = cargos.Find(car => car.IdCargo == idCargo).First<Cargo>();

            cargo.Descricao = descricao;
            cargo.CargaHoraria = cargahoraria;
            cargo.SalarioBase = salario;

            cargos.Update(cargo);
            try
            {
                cargos.SaveChanges();
                MessageBox.Show("Cargo alterado com sucesso!", MessageBox.MessageType.Success);
                return true;
            }
            catch
            {
                MessageBox.Show("Erro ao realizar operação!", MessageBox.MessageType.Error);
                return false;
            }


        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Delete)]
        public Boolean DeleteCargo(int idCargo)
        {
            CargoBo cargos = new CargoBo();
            Cargo cargo = new Cargo();

            cargo = cargos.Find(car => car.IdCargo == idCargo).First<Cargo>();


            cargos.Delete(cargo);
            try
            {
                cargos.SaveChanges();
                MessageBox.Show("Cargo excluido com sucesso!", MessageBox.MessageType.Success);
                return true;
            }
            catch
            {
                MessageBox.Show("Operação não permitida!", MessageBox.MessageType.Warning);
                return false;
            }

        }

    }
}